---------------------------------------------------------------------------------
-- @Description：  游戏工具函数([所有模块均可通用的函数写在sky_utils内]
--------------------------------------------------------------------------------
local skynet = require "skynet"
local uu = require "sky_utils"
local log = require "sky_log"
local serpent =  require "serpent"

local M = {
    --tarneeb最大人数为4
    MAX_PLAYER_TARNEEB = 4,
}

--是否第一回合
function M.check_is_round_1(current_round)
    if tonumber(current_round==1) then 
        return true
    end

    return false
end

--获取下一个叫分玩家座位
function M.get_call_score_pos(call_score_t)
    local pos = 1
    if not call_score_t then 
        pos = 1
    else
        pos = (#call_score_t % M.MAX_PLAYER_TARNEEB) + 1
    end

    return pos
end

--得到自己手中主牌数量
function M.get_main_card_count(handcards, main_color)
    return uu.get_color_cards_count(handcards, main_color)
end

--是否有多张主牌 手牌13时，6张及以上，小于13张时有半数以上
function M.check_has_more_maincard(handcards, main_color)
    local count = uu.get_color_cards_count(handcards, main_color)
    local handcards_count = #handcards
    if handcards_count==13 then
        if count >= 6 then
            return true
        end
    else
        if count >= (handcards_count/2) then
            return true
        end
    end

    return false
end

--得到手中最小的主牌
function M.get_handcards_min_main_card(handcards, main_color)
	local t = uu.get_handcards_one_color(handcards, main_color)

	table.sort(t, function (a, b)
		return a < b
	end)

	return t[1]
end

--得到最大的主牌并返回
function M.get_handcards_max_main_card(handcards, main_color)
	local t = uu.get_handcards_one_color(handcards, main_color)

	table.sort(t, function (a, b)
		return a > b
	end)

	return t[1]
end

--得到最大的牌并返回(先主再副)
function M.get_handcards_max_card(handcards, main_color)
	local t = M.get_handcards_one_color(handcards, main_color)
	table.sort(t, function (a, b)
		return a > b
	end)

	if t[1] then 
		return t[1]
	end

	local t2 = M.check_max_vice_card_t(handcards, main_color)
	table.sort(t2, function (a, b)
		return a.max_value > b.max_value
	end)

	return t2[1]
end

--是否手中有副牌并随机返回一张
function M.check_voice_card(handcards, main_color)
	local card, color
	for _, v in pairs(handcards) do
		color = uu.calc_card_color(v)
		if color~=tonumber(main_color) then 
			card = v
			break
		end
	end

	return card
end

--是否手中有最大的副牌并返回
function M.check_max_vice_card(handcards, main_color)
	local card, color, value, max_value
	for _, v in pairs(handcards) do
		color = uu.calc_card_color(v)
		if color~=tonumber(main_color) then 
			value = uu.calc_card_value(v)
			if not max_value then 
				max_value = value
				card = v
			end
			if value > max_value then 
				max_value = value
				card = v
			end
		end
	end

	return card
end

--得到手中 各副花色最大的牌 并返回
function M.check_max_vice_card_t(handcards, main_color)
	local t = {}
	local color, value
	for _, v in pairs(handcards) do
		color = uu.calc_card_color(v)
		if color~=tonumber(main_color) then 
			value = uu.calc_card_value(v)
			if not t[color] then 
				t[color] = {}
				t[color].max_value = 0
			end
			if value > t[color].max_value then 
				t[color].max_value = value
				t[color].card = v
				t[color].color = color
			end
		end
	end

	local ret_t = {}

	for _, v in pairs(t) do
		table.insert(ret_t, v)
	end

	return ret_t
end

--手中是否有单张副牌
function M.check_has_single_vice_card(handcards, main_color)
    local single_color_t = {}
	for i=1, 4 do 
		if i~=main_color then
			local t = uu.get_handcards_one_color(handcards, i)
			if #t and (#t == 1) then 
				table.insert(single_color_t, i)
			end
		end
	end

    
    return single_color_t
end

--手中是否除了一张副牌外全是主牌
function M.check_all_main_card_except_card(handcards, main_color)
	local t
	for i=1, 4 do 
		if i==main_color then 
			t = uu.get_handcards_one_color(handcards, i)
			break
		end
	end

	if #t and ( (#t + 1) == #handcards) then 
		return true
	end

	return false
end

--手牌是否除该花色和主牌外，无其他花色牌
function M.check_no_card_except_color_and_main_card(handcards, the_color, main_color)
	local color 
	for _, v in pairs(handcards) do
		color = uu.calc_card_color(v)
		if color~=the_color then 
			if color~=main_color then 
				return false
			end
		end
	end

	return true
end

--检测手牌中是否有比某张牌大的牌并返回其中最小的牌(不包含主牌)
function M.check_handcards_bigger_card_min_pure(handcards, the_card, round_color)
	local color = uu.calc_card_color(the_card)
	local value = uu.calc_card_value(the_card)

    if round_color~=color then
        local color_t = uu.get_handcards_one_color(handcards, round_color)
        if #color_t > 0 then
            return
        end
    end
    

	local t = {}

	local _color, _value
	for _, v in ipairs(handcards) do
		_color = uu.calc_card_color(v)
		if _color==color then 
			_value = uu.calc_card_value(v)
			if _value > value then 
				table.insert(t, v)
			end
		end
	end

	table.sort(t, function (a, b)
		return a < b
	end)

	return t[1]
end

--检测手牌中是否有比某张牌大的牌(不包含主牌)
function M.check_handcards_bigger_card_pure(handcards, the_card, round_color)
	local color = uu.calc_card_color(the_card)
	local value = uu.calc_card_value(the_card)

    if round_color~=color then
        local color_t = uu.get_handcards_one_color(handcards, round_color)
        if #color_t > 0 then
            return
        end
    end

	local _color, _value 
	--先比点数，有比 the_card大的就返回了
	for _, v in ipairs(handcards) do
		_color = uu.calc_card_color(v)
		if _color==color then 
			_value = uu.calc_card_value(v)
			if _value > value then 
				return true
			end
		end
	end

	return false
end

--得到手牌中是否有比某张牌大的牌(不包含主牌)
function M.get_handcards_bigger_card_pure(handcards, the_card, round_color)
	local color = uu.calc_card_color(the_card)
	local value = uu.calc_card_value(the_card)

    if round_color~=color then
        local color_t = uu.get_handcards_one_color(handcards, round_color)
        if #color_t > 0 then
            return
        end
    end

    local ret_t = {}
	local _color, _value 
	--先比点数，有比 the_card大的就返回了
	for _, v in ipairs(handcards) do
		_color = uu.calc_card_color(v)
		if _color==color then 
			_value = uu.calc_card_value(v)
			if _value > value then 
				table.insert(ret_t, v)
			end
		end
	end

	return ret_t
end

--检测是否有连续的牌
function M.check_is_lianxu(check_card, cards_t)
    local color = uu.calc_card_color(check_card)
    local value = uu.calc_card_value(check_card)

    local _color, _value
    for _, v in pairs(cards_t) do
        _color = uu.calc_card_color(v)
        _value = uu.calc_card_value(v)
        if color==_color then
            if (_value==value+1)
            or (_value==value-1) then
                return true
            end
        end
    end

    return false
end

--除主牌外检测出牌历史并从手牌中得到一张 花色出的最多的牌的最小牌（若花色均一样则出手里牌点数最小的）
function M.history_check_more_color_or_min_card_expect_maincards(handcards, outcard_history, main_color)
	local t =  uu.clone_table(handcards)
    table.sort(t, function (a, b)
		return a < b
	end)

    local t_2 = {}
    local _color
    for _, v in ipairs(t) do
        _color = uu.calc_card_color(v)
        if _color~=main_color then
            table.insert(t_2, v)
        end
    end
    if #t_2==0 then
        table.insert(t_2, t[1])
    end

	local card = uu.history_check_more_color_card(t_2, outcard_history)

	return card
end

--结合出牌历史：得到对手没有的花色的最大副牌表
function M.history_check_max_vice_card_t(handcards, main_color, enemy_no_color_t)
	local max_vice_cards_t = M.check_max_vice_card_t(handcards, main_color)

	local ret_t = {}
    for _, v in pairs(max_vice_cards_t) do
        if enemy_no_color_t[v.color]==true then 
			table.insert(ret_t, v)
        end
    end

	return ret_t
end

--结合出牌历史：得到手中有的多个副花色最大的牌(不包含主花色)
function M.history_check_max_vice_card_t_except_maincolor(handcards, main_color, outcard_history)
    local max_vice_cards_t = {}
    for i=1, 4 do
        if i~=main_color then
            local max_card_value = uu.history_check_max_card(handcards, i, outcard_history)
            if max_card_value then
                local temp = {}
                temp.color = i
                temp.value = max_card_value
                table.insert(max_vice_cards_t, temp)
            end
        end
    end

    return max_vice_cards_t
end

--历史出牌中 花色出的最少的的单张副牌
function M.history_check_less_color_vice_card(handcards, outcard_history, main_color, limit_colors)
    local _color_t = {false, false, false, false}
    for _, v in pairs(limit_colors) do
        _color_t[v] = true
    end

    local t = {}
	for _, v in pairs(handcards) do
        local color = uu.calc_card_color(v)
		if color~=main_color then
            --限定花色时
            if limit_colors and next(limit_colors) then
                if _color_t[color]==true then
                    table.insert(t, v)
                end
            else
                table.insert(t, v)
            end
		end
	end

	local card = uu.history_check_less_color_card(t, outcard_history)

	return card
end

--结合出牌历史： 是否手中有最大的主牌并返回
function M.check_max_main_card(handcards, main_color, outcard_history)
    local card_value = uu.history_check_max_card(handcards, main_color, outcard_history)
    if not card_value then 
        return
    end
    
    return uu.get_card_by_color_and_value(main_color, card_value)
end

--结合出牌历史：上一回合所出的牌是不是该花色中最大的牌
function M.check_last_rount_max_card(handcards, outcard_history, last_round_outcard)
	local ret = uu.history_check_the_max_card(handcards, outcard_history, last_round_outcard)

	return ret
end

--结合出牌历史：检测该花色牌是否只出了一回合
function M.check_card_color_out_1(last_round_outcard, outcard_history_more)
    local color = uu.calc_card_color(last_round_outcard)
    if uu.history_check_color_out_round(outcard_history_more, color)==1 then 
        return true
    end

    return false
end

--检测自己是否有 某花色牌, 并出该花色的最小牌
function M.check_self_has_color(handcards, no_color_t)
	local card
	for _, color in pairs(no_color_t) do
		local ret_t = uu.get_handcards_one_color(handcards, color)
		if next(ret_t) then 
			card = uu.get_color_min_card(handcards, color)
		end
	end

	return card
end

--除对手没有的花色外，是否有某种花色是第1，2轮出牌
function M.check_color_first_out_except_enemy(enemy_no_color_t, outcard_history_more, handcards)
    if not enemy_no_color_t
    or not outcard_history_more then
       return
    end
    
    local no_color_t = M.check_friend_has_no_color_t(enemy_no_color_t)
    --log.info("no_color_t=", no_color_t)
	if not next(no_color_t) then 
		--return
	end

    local color_count_t = {0, 0, 0, 0}

	local the_color
	for _, v in pairs(outcard_history_more) do
        local find_color_t = {}
        if v and v[1] and v[1].outcard then
            the_color = uu.calc_card_color(v[1].outcard)
            find_color_t[the_color] = true
        end

        for color, _v in pairs(find_color_t) do
            color_count_t[color] = color_count_t[color] + 1
        end
    end

    --log.info("color_count_t=", color_count_t)

    local find_color_t = {true, true, true, true}
    for _, v in pairs(no_color_t) do
        find_color_t[v] = false
    end

    local the_find_colors = {}
    for color, v in pairs(find_color_t) do
        if v==true then
            if color_count_t[color] and color_count_t[color] < 2 then
                table.insert(the_find_colors, color)
            end
        end
    end
    
    --log.info("before the_find_colors=", the_find_colors)
    --log.info("handcards=", handcards)
    --策划说且要包含条件：手里有该花色
    for k, v in pairs(the_find_colors) do
		local n = uu.get_color_cards_count(handcards, v)
		if n==0 then
			the_find_colors[k] = nil
		end
	end
    --log.info("after the_find_colors=", the_find_colors)

    return the_find_colors
end

--除对手没有的花色外，是否有某种花色是第3轮出牌
function M.check_color_third_out_except_enemy(enemy_no_color_t, outcard_history)
	local no_color_t = M.check_friend_has_no_color_t(enemy_no_color_t)
	if not next(no_color_t) then 
		--return
	end

    local color_count_t = {0, 0, 0, 0}

	local the_color
	for _, v in pairs(outcard_history_more) do
        local find_color_t = {}
        if v and v[1] and v[1].outcard then
            the_color = uu.calc_card_color(v[1].outcard)
            find_color_t[the_color] = true
        end

        for color, _v in pairs(find_color_t) do
            color_count_t[color] = color_count_t[color] + 1
        end
    end

    local find_color_t = {true, true, true, true}
    for _, v in pairs(no_color_t) do
        find_color_t[v] = false
    end

    local the_find_colors = {}
    for color, v in pairs(find_color_t) do
        if v==true then
            if color_count_t[color] and color_count_t[color] == 2 then
                table.insert(the_find_colors, color)
            end
        end
    end

    return the_find_colors
end

--检测对手没有某种花色 true:没有  其他(nil)有该花色
function M.check_enemy_has_no_color_bak(enemy_no_color_t, _color)
    return enemy_no_color_t[_color]
end

--fix_bug
function M.check_enemy_has_no_color(enemy_no_color_t, _color, m_seatid, outcard_history_more, only_next, handcards, outcard_history)
    if not m_seatid then
        return enemy_no_color_t[_color]
    end

    local the_enemy_no_color_t = {}
    for i=1, 4 do
        if (uu.check_is_friend(m_seatid, i)==false)
        and (i~=m_seatid) then
            the_enemy_no_color_t[i] = {}
            for _, v in pairs(outcard_history_more) do
                local round_color
                if v[1] and v[1].outcard then
                    round_color = uu.calc_card_color(v[1].outcard)
                end

                local check_color
                for _, _vv in pairs(v) do
                    if (_vv.seatid) and (tonumber(_vv.seatid)==i) then
                        check_color = uu.calc_card_color(_vv.outcard)
                    end
                end       

                if round_color and check_color then
                    if round_color~=check_color then
                        the_enemy_no_color_t[i][round_color] = true
                    end
                end

            end
        end
    end

    if handcards and outcard_history then
        for _the_color=1, 4 do
            local left_n = uu.history_check_color_left_cards_count(handcards, _the_color, outcard_history)
            if left_n==0 then
                for _, v in pairs(the_enemy_no_color_t) do
                    v[_the_color] = true
                end
            end
        end
    end

    --log.info("the_enemy_no_color_t=", the_enemy_no_color_t)

    if only_next and only_next==true then
        local next_seatid = (m_seatid % M.MAX_PLAYER_TARNEEB) + 1
        if the_enemy_no_color_t[next_seatid]
        and the_enemy_no_color_t[next_seatid][_color]
        and the_enemy_no_color_t[next_seatid][_color]==true then
            return true
        else
            return false
        end
    end

    for _, _the_enemy_no_color_t in pairs(the_enemy_no_color_t) do
        --log.info(" _the_enemy_no_color_t[_color]=",  _the_enemy_no_color_t[_color])
        if _the_enemy_no_color_t[_color]~=true then
            --log.info("_color=", _color)
            --log.info(" _the_enemy_no_color_t[_color]=",  _the_enemy_no_color_t[_color])
            return false
        end
    end

    return true
end

--检测是否 所有对手都没有主牌
function M.check_has_no_enemy_has_no_color(handcards, main_color, outcard_history, friend_no_color_t)
    local t = uu.history_check_color_left_cards_count(handcards, main_color, outcard_history)
    if t<1 then
        return true
    end

    if friend_no_color_t[main_color]==true then
        return false
    end

    return false
end


--检测对手是否没有某个花色牌且有主牌
function M.check_enemy_has_no_color_and_main_card(the_color, enemy_no_color_t, main_color)
    if enemy_no_color_t[the_color]==true then 
        if enemy_no_color_t[main_color]~=true then 
            return true
        end
    end

    return false
end

--检测对手是否没有某些花色牌且有主牌 (首先返回牌最多的那个花色)
function M.check_enemy_has_no_color_s_and_main_card(handcards, the_color_t, enemy_no_color_t, main_color)
	local color_t = {}

    for color, v in pairs(the_color_t) do
		if enemy_no_color_t[color]==true then 
        	if enemy_no_color_t[main_color]~=true then 
            	table.insert(color_t, color)
        	end
    	end
	end

    --log.info("color_t=", color_t)

    if next(color_t) then 
        local t = {0, 0, 0, 0}
        local _color 
        for _, v in pairs(handcards) do
            _color = uu.calc_card_color(v)
            t[_color] = t[_color] + 1
        end
        table.sort(t, function (a, b)
            return a > b
        end)

        for _color_, v in ipairs(t) do
            for _, the_color in pairs(color_t) do
                if _color_==the_color then 
                    return the_color
                end
            end
        end
    end
end

--得到我有，队手没有的花色 table
function M.check_enemy_has_no_self_color(handcards, enemy_no_color_t)
    local no_color_t = {}

    local color
    for _, v in pairs(handcards) do
        color = uu.calc_card_color(v)
        if enemy_no_color_t[color]==true then 
            no_color_t[color] = true
        end
    end

    return no_color_t
end

--检测队友是否没有主牌
function M.check_friend_has_no_main_card(friend_no_color_t, main_color)
    return friend_no_color_t[main_color]
end

--检测某张牌是否在队友手中
function M.check_card_in_friend_handcards(handcards, the_card, enemy_no_color_t)
	local ret_1 = uu.check_card_in_handcards(handcards, the_card)
	local color = uu.calc_card_color(the_card)
	local ret_2 = enemy_no_color_t[color]
	if (not ret_1) 
	and (ret_2==true) then
		return true
	end

	return false
end

--检测队友是否没有某个花色
function M.check_friend_has_no_color(friend_no_color_t)
    local ret
    for _, v in pairs(friend_no_color_t) do
        if v==true then
            return true
        end
    end
    return false
end

--检测队友没有的花色 table(对手也可以检测，命名有误导)
function M.check_friend_has_no_color_t(friend_no_color_t, main_color)
    local ret_t = {}
	for color, v in pairs(friend_no_color_t) do
        if v==true then
			table.insert(ret_t, color)
        end
    end

    if main_color then
        for k, v in pairs(ret_t) do
            if v==main_color then
                table.remove(ret_t, k)
                break
            end
        end
    end

    return ret_t
end

--得到我有，队友没有的花色 table
function M.check_friend_has_no_self_color(handcards, friend_no_color_t)
    local no_color_t = {}

    local color
    for _, v in pairs(handcards) do
        color = uu.calc_card_color(v)
        if friend_no_color_t[color]==true then 
            no_color_t[color] = true
        end
    end

    return no_color_t
end

--出自己手里 队友可能有的花色牌（若队友都没有，则随机出）
function M.do_out_friend_has_color(handcards, no_color_t)
    if #handcards==1 then
        return handcards[1]
    end

    local color_t = {}
    for i=1, 4 do
        if no_color_t[i] ~= true then 
            table.insert(color_t, i)
        end
    end

    if not next(color_t) then 
        return handcards[math.random(1, #handcards)]
    end

    for _, v in pairs(handcards) do
        local _color = uu.calc_card_color(v)
        for _, the_has_color in pairs(color_t) do
            if _color==the_has_color then
                return v
            end
        end
    end

   
    return handcards[math.random(1, #handcards)]
end

--出自己手里 队友没有该花色的牌（若队友都没有，则随机出）
function M.do_out_friend_has_no_color(handcards, no_color_t)
    local color 

    for _, v in pairs(handcards) do
        color = uu.calc_card_color(v)
        if no_color_t[color] == true then 
            return v
        end
    end
end

--得到上一回合第一个出的牌
function M.get_last_round_first_outcard(outcard_history_more)
    local current_round = #outcard_history_more
    if current_round <= 1 then 
        return 
    end 

    return outcard_history_more[current_round-1][1].outcard
end

--得到上一回合第一个出牌的座位号
function M.get_last_round_first_outcard_seatid(outcard_history_more)
    local current_round = #outcard_history_more
    if current_round 
    and outcard_history_more[current_round] 
    and #outcard_history_more[current_round]==4 then
        current_round = current_round + 1
    end

    if current_round <= 1 then
        return
    end

    return outcard_history_more[current_round-1][1].seatid
end

--tarneeb得到上一回合自己出的牌
function M.get_last_round_self_outcard(outcard_history_more, m_seatid)
    local current_round = #outcard_history_more
    if current_round 
    and outcard_history_more[current_round] 
    and #outcard_history_more[current_round]==4 then
        current_round = current_round + 1
    end

    if current_round <= 1 then 
        return
    end
  
    for _, v in pairs(outcard_history_more[current_round-1]) do
        if v.seatid == m_seatid then
            return v.outcard
        end
    end
end

--打出某张牌后要删除
function M.handcards_remove_card(handcards, read_handcards, card)
    if card==nil 
    or read_handcards[card]==nil then
        log.error("handcards_remove_card error card=", card)
        return 
    end

    for k, v in pairs(handcards) do
        if v==card then 
            table.remove(handcards, k)
            break
        end
    end

    read_handcards[card] = nil
end

--计算本回合是第几个出牌
function M.calc_round_pos(outcard_history_more)
    local pos 
    if not next(outcard_history_more) then 
        pos = 1
    end
    
    local round_t = outcard_history_more[#outcard_history_more]
    if #round_t==4 then 
        pos = 1
    else
        pos = #round_t + 1
    end

    return pos
end

--获得当前玩家的上一位玩家出牌
function M.get_prev_outcard(outcard_history_more)
    local card 
    if not next(outcard_history_more) then 
        return
    end

    local round_t = outcard_history_more[#outcard_history_more]
    if #round_t==4 then 
        return
    end
        
    card = round_t[#round_t].outcard
    
    return card
end

--获得本轮中最大的那张牌
function M.get_prev_max_outcard(outcard_history_more, main_color)
    if not next(outcard_history_more) then 
        return
    end

    local round_t = outcard_history_more[#outcard_history_more]
    if #round_t==4 then 
        return
    end

    if not round_t[1] 
    or not round_t[1].outcard then 
        return
    end

    local max_card = round_t[1].outcard
    local seatid = round_t[1].seatid

    local color, value, _color, _value
    for i=2, 4 do
        if round_t[i] and round_t[i].outcard then 
            color = uu.calc_card_color(round_t[i].outcard)
            value = uu.calc_card_value(round_t[i].outcard)
            
            _color = uu.calc_card_color(max_card)
            _value = uu.calc_card_value(max_card)

            if _color==main_color then 
                if color==main_color then 
                    if value > _value then 
                        max_card = round_t[i].outcard
                        seatid = round_t[i].seatid
                    end
                end
            else
                if color==_color then 
                    if value > _value then 
                        max_card = round_t[i].outcard
                        seatid = round_t[i].seatid
                    end
                else
                    if color==main_color then 
                        max_card = round_t[i].outcard
                        seatid = round_t[i].seatid
                    end
                end
            end
        end
    end

    return max_card, seatid
end

function M.get_handcards_less_color_max_card_in_colors(handcards, colors)
    local cout_t = {}
    for color, v in pairs(colors) do
        cout_t[color] = 0
        for _, _v in pairs(handcards) do
            local _color = uu.calc_card_color(_v)
            if _color==color then
                cout_t[color] = cout_t[color] + 1
            end
        end
    end

    local min_v = {
        color = nil,
        value = nil,
    }
    for color, count in pairs(cout_t) do
        if min_v.value==nil then
            min_v.value = count
            min_v.color = color
        end
        if count<min_v.value then
            min_v.value = count
            min_v.color = color
        end
    end

    local card
    if min_v.color then
        card = uu.get_color_max_card(handcards, min_v.color)
    end

    return card
end

function M.get_more_color_min_card_by_colors(handcards, colors)
    local t = {}
    for  _, v in pairs(colors) do
        local temp = {}
        temp.color = v
        temp.count = uu.get_color_cards_count(handcards, v)
        --当数量一致时才能随机出某花色
        local index = math.random(1, 100)
        if index<=50 then
            table.insert(t, 1, temp)
        else
            table.insert(t, temp)
        end
    end

    table.sort(t,function (a, b)
        return a.count > b.count
    end)
    --出花色数量多的最小牌
    local card = uu.get_color_min_card(handcards, t[1].color)

    return card
end

--得到手中花色最少的最大牌 (除某花色外)
function M.get_handcards_less_color_max_card_except_color(handcards, except_color)
	local color
	local count
	for i=1, 4 do
        if i~=except_color then
		    local t = uu.get_handcards_one_color(handcards, i)
		    if #t > 0 then
			    if not count then
				    count = #t
				    color = i
			    end
			    if count > #t then 
				    count = #t
				    color = i
			    end
		    end
        end
	end

    --此时只有主牌了
    if not color then
        color = except_color
    end

	local t2 = {}
	for _, v in pairs(handcards) do
		if uu.calc_card_color(v)==color then 
			table.insert(t2, v)
		end
	end

    --log.info("t2=", t2)
	table.sort(t2, function (a, b)
		return a>b
	end)

	return t2[1]
end

--============================================================================
--是否有本回合花色
function M.check_has_round_color(handcards, the_color)
    local color
    for _, v in pairs(handcards) do
        color = uu.calc_card_color(v)
        if color==the_color then 
            return true
        end
    end

    return false
end

--容错
function M.check_error_card(handcards, outcard, round_color, pos)
    if not handcards 
    or not pos then 
        log.error("check_error_card error!")
        return
    end

    if round_color==nil then
        log.error("[pos_"..pos.."] round_color nil!")
        local index = math.random(1, #handcards)
        round_color = uu.calc_card_color(handcards[index])
    end

    if outcard==nil then
        log.error("[pos_"..pos.."] outcard nil!")
        local _color
        for _, v in pairs(handcards) do
            _color = uu.calc_card_color(v)
            if _color==round_color then
                return true, v
            end
        end

        local index = math.random(1, #handcards)
        return true, handcards[index]
    end

    local the_color = uu.calc_card_color(outcard)
    if the_color~=round_color then
        if M.check_has_round_color(handcards, round_color) then
            log.error("[pos_"..pos.."] has_round color!")
            local card = uu.get_color_max_card(handcards, round_color)
            return true, card
        end
    end
end

return M